package com.blockchain.API;// Transaction.java

import org.web3j.abi.FunctionEncoder;
import org.web3j.abi.FunctionReturnDecoder;
import org.web3j.abi.TypeReference;
import org.web3j.abi.datatypes.*;
import org.web3j.crypto.*;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.DefaultBlockParameterName;
import org.web3j.protocol.core.methods.response.*;
import org.web3j.protocol.http.HttpService;
import org.web3j.tx.RawTransactionManager;
import org.web3j.tx.response.PollingTransactionReceiptProcessor;
import org.web3j.tx.response.TransactionReceiptProcessor;

import java.math.BigInteger;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class Test {

    private static final Web3j web3j = Web3j.build(new HttpService("http://47.108.212.117:8545"));
    //static String contractAddress = "0x42699a7612a82f1d9c36148af9c77354759b210b";
    // 计数器
    static String contractAddress = "0x43d1f9096674b5722d359b6402381816d5b22f28";
    static String adminPK = "0x8f2a55949038a9610f50fb23b5883af3b4ecb3c3bb792cbcefbd1542c692be63";
    static String testUsrPK = "8d383672cbd0f8301f8e32e110fdcd9403348323b307db854c544643609338aa";
    static Long chainId = 5201314L;
    static BigInteger gasLimit = BigInteger.valueOf(52401314L);
    // 愿意为每个 gas 支付的价格。即小费。
    static BigInteger gasPrice = BigInteger.valueOf(0L);
    // 小费
    static BigInteger value = BigInteger.valueOf(0L);

    public static void main(String[] args) throws Exception {

        //TimeCoinAPI.awardCoin(adminPK, testUsrPK, 20);
        //System.out.println(TimeCoinAPI.totalCoin(adminPK));
        //System.out.println(TimeCoinAPI.coinOfUser(testUsrPK, testUsrPK));


//        List<BigInteger> coinIds = new ArrayList<>();
//        for(int i = 2; i <= 20; i++){
//            coinIds.add(BigInteger.valueOf(i));
//        }
//        System.out.println(TimeCoinAPI.transferCoin(testUsrPK, testUsrPK, adminPK, coinIds, 20, "还给你"));

        //System.out.println(TimeCoinAPI.isAwardedCoin(adminPK, testUsrPK));
        //System.out.println(TimeCoinAPI.getCoinInfo(testUsrPK, BigInteger.valueOf(1L)));
        //System.out.println(TimeCoinAPI.getCoinInfoByHash(testUsrPK, "0xb10e2d527612073b26eecdfd717e6a320cf44b4afac2b0732d9fcbe2b7fa0cf6"));
        //System.out.println(TimeCoinAPI.getCoinTransferCnt(adminPK, BigInteger.valueOf(32L)));

        //deployContractMan();

    }

    private static void deployContractMan() throws Exception {
        Credentials credentials = Credentials.create(adminPK);
        // 读取合约二进制文件
        String contractBin = "60806040523480156200001157600080fd5b5060405162002434380380620024348339810160408190526200003491620001a3565b604051806040016040528060088152602001672a34b6b2a1b7b4b760c11b81525060405180604001604052806002815260200161544360f01b815250818181600090816200008391906200027c565b5060016200009282826200027c565b5050505050620000c97f403729048c52e3fdba3f9e3986daa85c85124b1c6427144f064f935ca837487982620000f060201b60201c565b50600b80546001600160a01b0319166001600160a01b039290921691909117905562000348565b6000828152600a602090815260408083206001600160a01b038516845290915281205460ff1662000199576000838152600a602090815260408083206001600160a01b03861684529091529020805460ff19166001179055620001503390565b6001600160a01b0316826001600160a01b0316847f2f8788117e7eff1d82e926ec794901d17c78024a50270940304540a733656f0d60405160405180910390a45060016200019d565b5060005b92915050565b600060208284031215620001b657600080fd5b81516001600160a01b0381168114620001ce57600080fd5b9392505050565b634e487b7160e01b600052604160045260246000fd5b600181811c908216806200020057607f821691505b6020821081036200022157634e487b7160e01b600052602260045260246000fd5b50919050565b601f82111562000277576000816000526020600020601f850160051c81016020861015620002525750805b601f850160051c820191505b8181101562000273578281556001016200025e565b5050505b505050565b81516001600160401b03811115620002985762000298620001d5565b620002b081620002a98454620001eb565b8462000227565b602080601f831160018114620002e85760008415620002cf5750858301515b600019600386901b1c1916600185901b17855562000273565b600085815260208120601f198616915b828110156200031957888601518255948401946001909101908401620002f8565b5085821015620003385787850151600019600388901b60f8161c191681555b5050505050600190811b01905550565b6120dc80620003586000396000f3fe608060405234801561001057600080fd5b50600436106102275760003560e01c806353d66f0111610130578063a3262e57116100b8578063c87b56dd1161007c578063c87b56dd146104c8578063d547741f146104db578063e985e9c5146104ee578063f403e36114610501578063ff8bc0541461051457600080fd5b8063a3262e5714610469578063b3e50ca71461047c578063b75cf6fa1461048f578063b88d4fde146104a2578063bd9578ea146104b557600080fd5b806381c69029116100ff57806381c690291461042057806391d148541461043357806395d89b4114610446578063a217fddf1461044e578063a22cb4651461045657600080fd5b806353d66f01146103d45780636352211e146103e757806370a08231146103fa5780637d2575941461040d57600080fd5b8063248a9ca3116101b357806339ce42271161018257806339ce4227146103805780633dee78001461038857806342842e0e1461038e5780634a2ba56f146103a15780634f6ccce7146103c157600080fd5b8063248a9ca3146103245780632f2ff15d146103475780632f745c591461035a57806336568abe1461036d57600080fd5b80630eafe473116101fa5780630eafe473146102a95780631498ce35146102ca5780631651659e146102f657806318160ddd1461030957806323b872dd1461031157600080fd5b806301ffc9a71461022c57806306fdde0314610254578063081812fc14610269578063095ea7b314610294575b600080fd5b61023f61023a3660046119d0565b610527565b60405190151581526020015b60405180910390f35b61025c610538565b60405161024b9190611a3d565b61027c610277366004611a50565b6105ca565b6040516001600160a01b03909116815260200161024b565b6102a76102a2366004611a80565b6105f3565b005b6102bc6102b7366004611aaa565b610602565b60405190815260200161024b565b61023f6102d8366004611aaa565b6001600160a01b03166000908152600c602052604090205460ff1690565b6102bc610304366004611a80565b61060d565b6008546102bc565b6102a761031f366004611ac5565b610620565b6102bc610332366004611a50565b6000908152600a602052604090206001015490565b6102a7610355366004611b01565b6106b0565b6102bc610368366004611a80565b6106d5565b6102a761037b366004611b01565b61073a565b6102bc610772565b436102bc565b6102a761039c366004611ac5565b610782565b6103b46103af366004611a50565b61079d565b60405161024b9190611b2d565b6102bc6103cf366004611a50565b610889565b6103b46103e2366004611a50565b6108e2565b61027c6103f5366004611a50565b61092c565b6102bc610408366004611aaa565b610937565b6102a761041b366004611bdd565b61097f565b6102a761042e366004611c3e565b6109c2565b61023f610441366004611b01565b610a4b565b61025c610a76565b6102bc600081565b6102a7610464366004611c9e565b610a85565b6102a7610477366004611a50565b610a90565b61025c61048a366004611a50565b610a9c565b61025c61049d366004611a50565b610aba565b6102a76104b0366004611cf0565b610ad5565b6102a76104c3366004611a80565b610aec565b61025c6104d6366004611a50565b610b9a565b6102a76104e9366004611b01565b610c0e565b61023f6104fc366004611dcc565b610c33565b6102bc61050f366004611a80565b610c61565b6102bc610522366004611a50565b610c88565b600061053282610c93565b92915050565b60606000805461054790611df6565b80601f016020809104026020016040519081016040528092919081815260200182805461057390611df6565b80156105c05780601f10610595576101008083540402835291602001916105c0565b820191906000526020600020905b8154815290600101906020018083116105a357829003601f168201915b5050505050905090565b60006105d582610cb8565b506000828152600460205260409020546001600160a01b0316610532565b6105fe828233610cf1565b5050565b600061053282610937565b600061061983836106d5565b9392505050565b6001600160a01b03821661064f57604051633250574960e11b8152600060048201526024015b60405180910390fd5b600061065c838333610cfe565b9050836001600160a01b0316816001600160a01b0316146106aa576040516364283d7b60e01b81526001600160a01b0380861660048301526024820184905282166044820152606401610646565b50505050565b6000828152600a60205260409020600101546106cb81610dd3565b6106aa8383610ddd565b60006106e083610937565b82106107115760405163295f44f760e21b81526001600160a01b038416600482015260248101839052604401610646565b506001600160a01b03919091166000908152600660209081526040808320938352929052205490565b6001600160a01b03811633146107635760405163334bd91960e11b815260040160405180910390fd5b61076d8282610e71565b505050565b600061077d60085490565b905090565b61076d83838360405180602001604052806000815250610ad5565b6060600d6000838152602001908152602001600020805480602002602001604051908101604052809291908181526020016000905b8282101561087e5783829060005260206000200180546107f190611df6565b80601f016020809104026020016040519081016040528092919081815260200182805461081d90611df6565b801561086a5780601f1061083f5761010080835404028352916020019161086a565b820191906000526020600020905b81548152906001019060200180831161084d57829003601f168201915b5050505050815260200190600101906107d2565b505050509050919050565b600061089460085490565b82106108bd5760405163295f44f760e21b81526000600482015260248101839052604401610646565b600882815481106108d0576108d0611e30565b90600052602060002001549050919050565b6000818152600f60209081526040808320548352600e909152902054606090820361091f576000828152600f60205260409020546105329061079d565b506060919050565b919050565b600061053282610cb8565b60006001600160a01b038216610963576040516322718ad960e21b815260006004820152602401610646565b506001600160a01b031660009081526003602052604090205490565b60005b818110156109bb57600083838381811061099e5761099e611e30565b9050602002013590506109b2868683610782565b50600101610982565b5050505050565b60005b838110156109bb5760008585838181106109e1576109e1611e30565b905060200201359050600d6000828152602001908152602001600020848484818110610a0f57610a0f611e30565b9050602002810190610a219190611e46565b82546001810184556000938452602090932090920191610a419183611edd565b50506001016109c5565b6000918252600a602090815260408084206001600160a01b0393909316845291905290205460ff1690565b60606001805461054790611df6565b6105fe338383610ede565b610a9981610f7d565b50565b60606000610619610aac8461092c565b6001600160a01b0316610fb8565b6000818152600d602052604090205460609061053290610fce565b610ae0848484610620565b6106aa84848484611061565b610b167f403729048c52e3fdba3f9e3986daa85c85124b1c6427144f064f935ca837487933610a4b565b610b3557604051630481400f60e31b8152336004820152602401610646565b6001600160a01b0382166000908152600c60205260408120805460ff19166001179055610b60610772565b905060005b828110156106aa57610b8084610b7b8385611fb3565b611183565b610b92610b8d8284611fb3565b61119d565b600101610b65565b6060610ba582610cb8565b506000610bbd60408051602081019091526000815290565b90506000815111610bdd5760405180602001604052806000815250610619565b80610be784610fce565b604051602001610bf8929190611fc6565b6040516020818303038152906040529392505050565b6000828152600a6020526040902060010154610c2981610dd3565b6106aa8383610e71565b6001600160a01b03918216600090815260056020908152604080832093909416825291909152205460ff1690565b6000600e6000610c7185856106d5565b815260200190815260200160002054905092915050565b600061053282610889565b60006001600160e01b03198216637965db0b60e01b14806105325750610532826111ea565b6000818152600260205260408120546001600160a01b03168061053257604051637e27328960e01b815260048101849052602401610646565b61076d838383600161120f565b600080610d0c858585611315565b90506001600160a01b038116610d6957610d6484600880546000838152600960205260408120829055600182018355919091527ff3f7a9fe364faab93b216da50a3214154f22a0a2b415b23a84c8169e8b636ee30155565b610d8c565b846001600160a01b0316816001600160a01b031614610d8c57610d8c818561140e565b6001600160a01b038516610da857610da38461149f565b610dcb565b846001600160a01b0316816001600160a01b031614610dcb57610dcb858561154e565b949350505050565b610a99813361159e565b6000610de98383610a4b565b610e69576000838152600a602090815260408083206001600160a01b03861684529091529020805460ff19166001179055610e213390565b6001600160a01b0316826001600160a01b0316847f2f8788117e7eff1d82e926ec794901d17c78024a50270940304540a733656f0d60405160405180910390a4506001610532565b506000610532565b6000610e7d8383610a4b565b15610e69576000838152600a602090815260408083206001600160a01b0386168085529252808320805460ff1916905551339286917ff6391f5c32d9c69d2a47ea670b442974b53935d1edc7fd64eb21e047a839171b9190a4506001610532565b6001600160a01b038216610f1057604051630b61174360e31b81526001600160a01b0383166004820152602401610646565b6001600160a01b03838116600081815260056020908152604080832094871680845294825291829020805460ff191686151590811790915591519182527f17307eab39ab6107e8899845ad3d59bd9653f200f220920489ca2b5937696c31910160405180910390a3505050565b6000610f8c6000836000610cfe565b90506001600160a01b0381166105fe57604051637e27328960e01b815260048101839052602401610646565b60606105326001600160a01b03831660146115d7565b60606000610fdb8361174f565b600101905060008167ffffffffffffffff811115610ffb57610ffb611cda565b6040519080825280601f01601f191660200182016040528015611025576020820181803683370190505b5090508181016020015b600019016f181899199a1a9b1b9c1cb0b131b232b360811b600a86061a8153600a850494508461102f57509392505050565b6001600160a01b0383163b156106aa57604051630a85bd0160e11b81526001600160a01b0384169063150b7a02906110a3903390889087908790600401611ff5565b6020604051808303816000875af19250505080156110de575060408051601f3d908101601f191682019092526110db91810190612032565b60015b611147573d80801561110c576040519150601f19603f3d011682016040523d82523d6000602084013e611111565b606091505b50805160000361113f57604051633250574960e11b81526001600160a01b0385166004820152602401610646565b805181602001fd5b6001600160e01b03198116630a85bd0160e11b146109bb57604051633250574960e11b81526001600160a01b0385166004820152602401610646565b6105fe828260405180602001604052806000815250611827565b6000816040516020016111b291815260200190565b60408051601f1981840301815291815281516020928301206000858152600e8452828120829055908152600f90925290209190915550565b60006001600160e01b0319821663780e9d6360e01b148061053257506105328261183e565b808061122357506001600160a01b03821615155b156112e557600061123384610cb8565b90506001600160a01b0383161580159061125f5750826001600160a01b0316816001600160a01b031614155b801561127257506112708184610c33565b155b1561129b5760405163a9fbf51f60e01b81526001600160a01b0384166004820152602401610646565b81156112e35783856001600160a01b0316826001600160a01b03167f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b92560405160405180910390a45b505b5050600090815260046020526040902080546001600160a01b0319166001600160a01b0392909216919091179055565b6000828152600260205260408120546001600160a01b03908116908316156113425761134281848661188e565b6001600160a01b038116156113805761135f60008560008061120f565b6001600160a01b038116600090815260036020526040902080546000190190555b6001600160a01b038516156113af576001600160a01b0385166000908152600360205260409020805460010190555b60008481526002602052604080822080546001600160a01b0319166001600160a01b0389811691821790925591518793918516917fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef91a4949350505050565b600061141983610937565b60008381526007602052604090205490915080821461146c576001600160a01b03841660009081526006602090815260408083208584528252808320548484528184208190558352600790915290208190555b5060009182526007602090815260408084208490556001600160a01b039094168352600681528383209183525290812055565b6008546000906114b19060019061204f565b600083815260096020526040812054600880549394509092849081106114d9576114d9611e30565b9060005260206000200154905080600883815481106114fa576114fa611e30565b600091825260208083209091019290925582815260099091526040808220849055858252812055600880548061153257611532612062565b6001900381819060005260206000200160009055905550505050565b6000600161155b84610937565b611565919061204f565b6001600160a01b039093166000908152600660209081526040808320868452825280832085905593825260079052919091209190915550565b6115a88282610a4b565b6105fe5760405163e2517d3f60e01b81526001600160a01b038216600482015260248101839052604401610646565b60608260006115e7846002612078565b6115f2906002611fb3565b67ffffffffffffffff81111561160a5761160a611cda565b6040519080825280601f01601f191660200182016040528015611634576020820181803683370190505b509050600360fc1b8160008151811061164f5761164f611e30565b60200101906001600160f81b031916908160001a905350600f60fb1b8160018151811061167e5761167e611e30565b60200101906001600160f81b031916908160001a90535060006116a2856002612078565b6116ad906001611fb3565b90505b6001811115611725576f181899199a1a9b1b9c1cb0b131b232b360811b83600f16601081106116e1576116e1611e30565b1a60f81b8282815181106116f7576116f7611e30565b60200101906001600160f81b031916908160001a90535060049290921c9161171e8161208f565b90506116b0565b508115610dcb5760405163e22e27eb60e01b81526004810186905260248101859052604401610646565b60008072184f03e93ff9f4daa797ed6e38ed64bf6a1f0160401b831061178e5772184f03e93ff9f4daa797ed6e38ed64bf6a1f0160401b830492506040015b6d04ee2d6d415b85acef810000000083106117ba576d04ee2d6d415b85acef8100000000830492506020015b662386f26fc1000083106117d857662386f26fc10000830492506010015b6305f5e10083106117f0576305f5e100830492506008015b612710831061180457612710830492506004015b60648310611816576064830492506002015b600a83106105325760010192915050565b61183183836118f2565b61076d6000848484611061565b60006001600160e01b031982166380ac58cd60e01b148061186f57506001600160e01b03198216635b5e139f60e01b145b8061053257506301ffc9a760e01b6001600160e01b0319831614610532565b611899838383611957565b61076d576001600160a01b0383166118c757604051637e27328960e01b815260048101829052602401610646565b60405163177e802f60e01b81526001600160a01b038316600482015260248101829052604401610646565b6001600160a01b03821661191c57604051633250574960e11b815260006004820152602401610646565b600061192a83836000610cfe565b90506001600160a01b0381161561076d576040516339e3563760e11b815260006004820152602401610646565b60006001600160a01b03831615801590610dcb5750826001600160a01b0316846001600160a01b0316148061199157506119918484610c33565b80610dcb5750506000908152600460205260409020546001600160a01b03908116911614919050565b6001600160e01b031981168114610a9957600080fd5b6000602082840312156119e257600080fd5b8135610619816119ba565b60005b83811015611a085781810151838201526020016119f0565b50506000910152565b60008151808452611a298160208601602086016119ed565b601f01601f19169290920160200192915050565b6020815260006106196020830184611a11565b600060208284031215611a6257600080fd5b5035919050565b80356001600160a01b038116811461092757600080fd5b60008060408385031215611a9357600080fd5b611a9c83611a69565b946020939093013593505050565b600060208284031215611abc57600080fd5b61061982611a69565b600080600060608486031215611ada57600080fd5b611ae384611a69565b9250611af160208501611a69565b9150604084013590509250925092565b60008060408385031215611b1457600080fd5b82359150611b2460208401611a69565b90509250929050565b600060208083016020845280855180835260408601915060408160051b87010192506020870160005b82811015611b8457603f19888603018452611b72858351611a11565b94509285019290850190600101611b56565b5092979650505050505050565b60008083601f840112611ba357600080fd5b50813567ffffffffffffffff811115611bbb57600080fd5b6020830191508360208260051b8501011115611bd657600080fd5b9250929050565b60008060008060608587031215611bf357600080fd5b611bfc85611a69565b9350611c0a60208601611a69565b9250604085013567ffffffffffffffff811115611c2657600080fd5b611c3287828801611b91565b95989497509550505050565b60008060008060408587031215611c5457600080fd5b843567ffffffffffffffff80821115611c6c57600080fd5b611c7888838901611b91565b90965094506020870135915080821115611c9157600080fd5b50611c3287828801611b91565b60008060408385031215611cb157600080fd5b611cba83611a69565b915060208301358015158114611ccf57600080fd5b809150509250929050565b634e487b7160e01b600052604160045260246000fd5b60008060008060808587031215611d0657600080fd5b611d0f85611a69565b9350611d1d60208601611a69565b925060408501359150606085013567ffffffffffffffff80821115611d4157600080fd5b818701915087601f830112611d5557600080fd5b813581811115611d6757611d67611cda565b604051601f8201601f19908116603f01168101908382118183101715611d8f57611d8f611cda565b816040528281528a6020848701011115611da857600080fd5b82602086016020830137600060208483010152809550505050505092959194509250565b60008060408385031215611ddf57600080fd5b611de883611a69565b9150611b2460208401611a69565b600181811c90821680611e0a57607f821691505b602082108103611e2a57634e487b7160e01b600052602260045260246000fd5b50919050565b634e487b7160e01b600052603260045260246000fd5b6000808335601e19843603018112611e5d57600080fd5b83018035915067ffffffffffffffff821115611e7857600080fd5b602001915036819003821315611bd657600080fd5b601f82111561076d576000816000526020600020601f850160051c81016020861015611eb65750805b601f850160051c820191505b81811015611ed557828155600101611ec2565b505050505050565b67ffffffffffffffff831115611ef557611ef5611cda565b611f0983611f038354611df6565b83611e8d565b6000601f841160018114611f3d5760008515611f255750838201355b600019600387901b1c1916600186901b1783556109bb565b600083815260209020601f19861690835b82811015611f6e5786850135825560209485019460019092019101611f4e565b5086821015611f8b5760001960f88860031b161c19848701351681555b505060018560011b0183555050505050565b634e487b7160e01b600052601160045260246000fd5b8082018082111561053257610532611f9d565b60008351611fd88184602088016119ed565b835190830190611fec8183602088016119ed565b01949350505050565b6001600160a01b038581168252841660208201526040810183905260806060820181905260009061202890830184611a11565b9695505050505050565b60006020828403121561204457600080fd5b8151610619816119ba565b8181038181111561053257610532611f9d565b634e487b7160e01b600052603160045260246000fd5b808202811582820484141761053257610532611f9d565b60008161209e5761209e611f9d565b50600019019056fea264697066735822122082830ce044ba2462ffdfe1d8e7dcc1bc27f67ce5a7bb8e994b64085af86af20164736f6c63430008180033";
        String encodedConstructor = FunctionEncoder.encodeConstructor(Arrays.<Type>asList(new Address(160, credentials.getAddress())));
        String data = contractBin + encodedConstructor;

        // 这是不需要准备原始交易，直接进行交易的快速方法
        RawTransactionManager rawManager = new RawTransactionManager(web3j, credentials, chainId);
        EthSendTransaction ethSendTransaction = rawManager.sendTransaction(gasPrice, gasLimit, null, data, BigInteger.ZERO);
        // 等待交易被挖矿
        TransactionReceiptProcessor receiptProcessor = new PollingTransactionReceiptProcessor(web3j, 1000, 60);
        TransactionReceipt receipt = receiptProcessor.waitForTransactionReceipt(ethSendTransaction.getTransactionHash());
        // 打印交易结果
        contractAddress = receipt.getContractAddress();
        System.out.println("Success? :" + receipt.isStatusOK());
        System.out.println("Transaction hash: " + receipt.getTransactionHash());
        System.out.println("Contract address: " + receipt.getContractAddress());
    }

    private static void testContractTransction(Web3j web3j, Credentials credentials) throws Exception {
        Function function = new Function(
                "increment",  // function we're calling
                Collections.emptyList(),  // Parameters to pass as Solidity Types
                Collections.emptyList()
        );

        String encodedFunction = FunctionEncoder.encode(function);

        RawTransactionManager rawManager = new RawTransactionManager(web3j, credentials, chainId);
        EthSendTransaction ethSendTransaction = rawManager.sendTransaction(gasPrice, gasLimit, contractAddress, encodedFunction, BigInteger.ZERO);

        // 等待交易被挖矿
        TransactionReceiptProcessor receiptProcessor = new PollingTransactionReceiptProcessor(web3j, 1000, 60);
        TransactionReceipt receipt = receiptProcessor.waitForTransactionReceipt(ethSendTransaction.getTransactionHash());
        // 打印交易结果
        contractAddress = receipt.getContractAddress();
        System.out.println("Success? :" + receipt.isStatusOK());
        System.out.println("Transaction hash: " + receipt.getTransactionHash());
    }

    private static void testContractCall(Web3j web3j, Credentials credentials) throws Exception {

        Function function = new Function(
                "getCount",
                Collections.emptyList(),  // Solidity Types in smart contract functions
                Arrays.asList(new TypeReference<Uint>() {
                })
        );

        String encodedFunction = FunctionEncoder.encode(function);

        RawTransactionManager rawTransactionManager = new RawTransactionManager(web3j, credentials, chainId);
        String out = rawTransactionManager.sendCall(contractAddress, encodedFunction, DefaultBlockParameterName.LATEST);
        List<Type> someTypes = FunctionReturnDecoder.decode(out, function.getOutputParameters());

        // 输出结果
        for (Type type : someTypes) {
            System.out.println(type.getValue());
        }
    }
}
